#!/bin/bash
TARGET_LOG="/var/log/nginx/error.log"
KEYWORDS=("500" "404" "timeout")

# 颜色定义
RED='\033[31m'
GREEN='\033[32m'
NC='\033[0m'

tail -Fn0 "$TARGET_LOG" | while read line; do
    for keyword in "${KEYWORDS[@]}"; do
        if echo "$line" | grep -q "$keyword"; then
            echo -e "$(date +%T) ${RED}[ALERT]${NC} 检测到关键词 ${RED}$keyword${NC}: $line" | tee -a /var/log/error_monitor.log
            # 可扩展：发送邮件/短信告警
        fi
    done
done
#功能：实时监控多关键词 ，高亮显示关键错误 ，支持动态添加关键词